package com.appspot.niehonglei.action;

import java.util.Calendar;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.niehonglei.common.Tools;
import com.appspot.niehonglei.dao.PMF;
import com.appspot.niehonglei.jdo.Comment;
import com.google.appengine.api.datastore.Text;

public class SaveComment implements ActionInterface {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {

		if(true){
			return;
		}
		String indexId = request.getParameter("indexId");
		String nickName = request.getParameter("nickName");
		String email = request.getParameter("email");
		String content = request.getParameter("content");
		String personalHomePage = request.getParameter("personalHomePage");
		String ipAddress = request.getRemoteAddr();
		String ifPublish = request.getParameter("ifPublish");
		boolean ifSpam = false;
		Date lastCommentTime = (Date) request.getSession().getAttribute(
				"lastCommentTime");
		if (lastCommentTime != null) {
			ifSpam = lastCommentTime.after(new Date());
		}
		String url = "/";
		try {
			if (!"".equals(Tools.dealNull(content))
					&& !"".equals(Tools.dealNull(nickName))
					&& !"".equals(Tools.dealNull(email))
					&& !"".equals(Tools.dealNull(indexId)) && !ifSpam
					&& Tools.checkEmail(email)) {
				if (content.length() <= 1000 && nickName.length() <= 20
						&& email.length() <= 50) {

					nickName = Tools.replaceTag(nickName);
					content = Tools.replaceTag(content);
					PersistenceManager pm = PMF.get().getPersistenceManager();
					Comment comment = new Comment(Long.valueOf(indexId),
							nickName, email, new Text(content), ipAddress);
					if (!"".equals(Tools.dealNull(personalHomePage))) {
						if (personalHomePage.indexOf("http://") != 0) {
							personalHomePage = "http://" + personalHomePage;
						}
						personalHomePage = Tools.replaceTag(personalHomePage);
						comment.setPersonalHomePage(personalHomePage);
					}
					if ("false".equals(ifPublish)) {
						comment.setIfPublish(ifPublish);
					}
					pm.makePersistent(comment);
					pm.close();

				}
			}
			Date relayTime = new Date();
			String count = (String) request.getSession().getAttribute("count");
			if (count == null) {
				count = "0";
			}
			int intCount = Integer.parseInt(count);
			int addMinute = 1;
			if (ifSpam) {
				intCount ++;
				addMinute = intCount * intCount;
			}
			Calendar c = Calendar.getInstance();
			c.add(Calendar.MINUTE, addMinute);
			relayTime = c.getTime();
			count = intCount + "";
			request.getSession().setAttribute("lastCommentTime", relayTime);
			request.getSession().setAttribute("count", count);
			if (!"".equals(Tools.dealNull(indexId))) {
				url = "/detail/" + indexId;
			}
			response.sendRedirect(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
